package seso;

import javax.swing.*;
import java.awt.*;

/**
 *
 * @author Administrator
 */
public class RImageBox extends JLabel {

    Image image = null;
    int drawX = 0, drawY = 0, drawWidth = 0, drawHeight = 0, imageWidth = 0, imageHeight = 0;
    int fillMethod = 0;

    public RImageBox() {
        super();
        setOpaque(false);  // Must be false
    }

    public void setImage(Image image) {
        this.image = image;
        imageWidth = image.getWidth(this);
        imageHeight = image.getHeight(this);
    }

    public Image getImage() {
        return image;
    }

    public void setFillMethod(int fillMethod) {
        this.fillMethod = fillMethod;
    }
    
    

    @Override
    protected void paintComponent(Graphics g) {

        int width = getWidth();
        int height = getHeight();

        if (image != null && imageWidth > 0 && imageHeight > 0) {
            drawWidth = width;
            drawHeight = height;

            if (fillMethod != 0) {

                drawHeight = (int) (((float) width / (float) imageWidth) * imageHeight);
                if (drawHeight <= height) {
                    drawWidth = width;
                } else {
                    drawWidth = (int) (((float) height / (float) imageHeight) * imageWidth);
                    drawHeight = height;
                }
            }

            g.drawImage(image, drawX, drawY, drawWidth, drawHeight, null);

        }
        super.paintComponent(g);
    }

    
}
